ansible ssh 設定
ansibleはデフォルトではユーザーの ~/.ssh/config を使わずにssh接続を試みる。
#TBD 要確認(たしかそうだったと思うんだけどーーー)
ssh-addで鍵を登録
ssh-addでssh-agentに登録した鍵は使ってくれる
対象サーバーによっては、鍵が合わない、鍵の試行回数が多くなりすぎて接続に失敗する、といった状態が発生する
ssh: Too many authentication failures
ssh鍵を指定
鍵が対象ホスト群で共通であれば、鍵をansible-playbookコマンドに指定することで解決する
code:bash
$ ansible-playbook --private-key=~/.ssh/key.pem -i hosts site.yml
ssh config を明示的に指定
sshのオプションに以下のように明示的にssh_configを渡せる
code: bash
$ ssh -F ~/.ssh/config hostname
そこで、ansible.cfg に以下のように専用のssh_configを指定する
code:ansible.cfg
ssh_connection
ssh_args = -F ./path/to/ssh_config
ansible.cfg の置き場所はいくつかあります
https://docs.ansible.com/ansible/latest/reference_appendices/config.html#the-configuration-file
このssh_configファイルにサーバー別の接続設定を書いておく
もしansible.cfgを書きたくない場合は、環境変数 ANSIBLE_SSH_ARGSでも指定できる
code:bash
$ ANSIBLE_SSH_ARGS='-F ~/.ssh/config' ansible-playbook .....
inventoryファイル内にホスト別の鍵設定を指定する
この方法は、鍵の位置を全て固定化するので、複数人で共有しづらい
code:inventory.yml
all:
hosts:
mail.example.com:
ansible_ssh_private_key_file: ~/.ssh/key1
children:
webservers:
hosts:
foo.example.com:
ansible_ssh_private_key_file: ~/.ssh/key2
bar.example.com:
ansible_ssh_private_key_file: ~/.ssh/key3
code:inventory.ini
hosts
mail.example.com ansible_ssh_private_key_file=~/.ssh/key1
webservers:children
foo.example.com ansible_ssh_private_key_file=~/.ssh/key2
bar.example.com ansible_ssh_private_key_file=~/.ssh/key3
参考
ansible.cnfでssh_configを設定する | DevelopersIO